package com.scwatch.ext.web.back;

import com.scwatch.common.web.Servlets;
import com.scwatch.core.constant.Constants;
import com.scwatch.core.domain.Site;
import com.scwatch.core.domain.User;
import com.scwatch.core.support.Backends;
import com.scwatch.core.support.Context;
import com.scwatch.ext.domain.Qrcode;
import com.scwatch.ext.service.QrcodeService;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

import static com.scwatch.core.constant.Constants.*;

/**
 * @author  gjl
 */
@Controller
@RequestMapping("/ext/qrcode")
public class QrcodeController {
    // 日志记录器
    public final Logger log = Logger.getLogger(this.getClass());


    @Autowired
    private QrcodeService qrcodeService;

    @RequiresPermissions("ext:qrcode:list")
    @RequestMapping("list.do")
    public String list(@PageableDefault(sort = { "createDate", "id" }, direction = Sort.Direction.DESC) Pageable pageable,
                       HttpServletRequest request, org.springframework.ui.Model modelMap) {

        Site site = Context.getCurrentSite();

        Map<String, String[]> params = Servlets.getParamValuesMap(request, Constants.SEARCH_PREFIX);

        Page<Qrcode> pagedList = qrcodeService.findAll(site.getId(), params, pageable);

        modelMap.addAttribute("pagedList", pagedList);

        return "ext/qrcode/qrcode_list";
    }

    @RequiresPermissions("ext:qrcode:create")
    @RequestMapping("create.do")
    public String create(Integer position, HttpServletRequest request, org.springframework.ui.Model modelMap) {

        modelMap.addAttribute("position", position);

        modelMap.addAttribute(OPRT, CREATE);

        return "ext/qrcode/qrcode_form";

    }

    @RequiresPermissions("ext:qrcode:edit")
    @RequestMapping("edit.do")
    public String edit(Integer id, Integer position,
                       HttpServletRequest request, org.springframework.ui.Model modelMap) {

        Qrcode bean = qrcodeService.get(id);

        Map<String, String[]> params = Servlets.getParamValuesMap(request,
                Constants.SEARCH_PREFIX);

        modelMap.addAttribute("bean", bean);

        modelMap.addAttribute("position", position);

        modelMap.addAttribute(OPRT, EDIT);

        return "ext/qrcode/qrcode_form";
    }

    @RequiresPermissions("ext:qrcode:save")
    @RequestMapping("save.do")
    public String save(Integer id, Integer position, Qrcode bean,
                       HttpServletRequest request, String redirect, RedirectAttributes ra) {

        Site site = Context.getCurrentSite();
        User user = Context.getCurrentUser();
        bean.setSite(site);
        bean.setUser(user);
        bean.setCreateDate(new Date());


        bean = qrcodeService.save(bean);

        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);

        if (Constants.REDIRECT_LIST.equals(redirect)) {

            ra.addAttribute("position", position);

            return "redirect:list.do";

        }
        if(Constants.REDIRECT_EDIT.equals(redirect)){

            ra.addAttribute("id", bean.getId());

            return "redirect:edit.do";

        }else{
            return "redirect:create.do";
        }
    }

    @RequiresPermissions("ext:qrcode:update")
    @RequestMapping("update.do")
    public String update( Integer position, String redirect, Qrcode bean,
                          HttpServletRequest request, RedirectAttributes ra) {

        Site site = Context.getCurrentSite();

        User user = Context.getCurrentUser();

        Backends.validateDataInSite(bean, site.getId());

        bean.setUser(user);
        bean.setSite(site);
        bean.setUdpateDate(new Date());
        if(bean!=null && bean.getId()!=null){
            Qrcode v=qrcodeService.get(bean.getId());
            bean.setCreateDate(v.getCreateDate());
        }

        bean = qrcodeService.update(bean);

        ra.addFlashAttribute(MESSAGE, SAVE_SUCCESS);

        if (Constants.REDIRECT_LIST.equals(redirect)) {

            ra.addAttribute("position", position);

            return "redirect:list.do";

        }
        if(Constants.REDIRECT_CREATE.equals(redirect)){
            return "redirect:create.do";
        }else {
            ra.addAttribute("id", bean.getId());
            return "redirect:edit.do";
        }
    }


    @RequiresPermissions("ext:qrcode:delete")
    @RequestMapping("delete.do")
    public String delete(Integer[] ids, HttpServletRequest request,
                         RedirectAttributes ra) {

        Site site = Context.getCurrentSite();

        validateIds(ids, site.getId());

        qrcodeService.delete(ids);

        ra.addFlashAttribute(MESSAGE, DELETE_SUCCESS);

        return "redirect:list.do";
    }

    private void validateIds(Integer[] ids, Integer siteId) {

        for (Integer id : ids) {

            Backends.validateDataInSite(qrcodeService.get(id), siteId);
        }
    }


}
